package org.me.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwt.components.client.Canvas;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Tests01 implements EntryPoint, ClickListener, KeyboardListener {

	int MAXITS = 200;

	double[][] A;

	final DockPanel main = new DockPanel();

	final Button matriz_ButtonDeNMasMas = new Button("n + +", this);

	final Button matriz_ButtonDeNMenosMenos = new Button("n - -", this);

	final VerticalPanel matriz_ColumnaDeFilas = new VerticalPanel();

	final HorizontalPanel matriz_HorizontalPanel_ParaEcuacioon = new HorizontalPanel();

	final VerticalPanel matriz_HorizontalPanel_VerticalPanel_ConBotones = new VerticalPanel();

	final Label matriz_Label_ConNombreDeMat = new Label("A");

	final ScrollPanel matriz_ScrollPanelDeColumnaDeFilas = new ScrollPanel();

	private int MAX_N = 100;

	private int N = 5;

	final DockPanel plotFlaeche = new DockPanel();

	final TextBox tabpanel_tab1_TB_plotFunc = new TextBox();

	final TextBox tabpanel_tab1_TB_plotVar = new TextBox();

	final TabPanel tabPanel = new TabPanel();

	final Button tabpanel_tab0_Button_csv = new Button("Inv(A), CSV", this);

	final Button tabpanel_tab0_Button_Eff = new Button("Eff", this);

	final Button tabpanel_tab0_Button_Init = new Button("init", this);

	final Button tabpanel_tab0_Button_Inv = new Button("Inv(A)", this);

	final Button tabpanel_tab0_Button_nmasmas = new Button();

	final Button tabpanel_tab0_Button_nmenosmenos = new Button();

	final HorizontalPanel tabpanel_tab0_HorizontalPanel_button = new HorizontalPanel();

	private TextBox[][] tabpanel_tab0_matriz_TextBoxes = generaTextBoxes(N, N,
			"1cm");

	final CheckBox tabpanel_tab0_randomFill = new CheckBox("random?");

	final Label tabpanel_tab0_restrict = new Label("1<n<100");

	final TextArea tabpanel_tab0_textArea = new TextArea();

	final VerticalPanel tabpanel_tab0_VerticalPanel = new VerticalPanel();

	final Button tabpanel_tab1_Button_plotme = new Button("plotme", this);

	final HorizontalPanel tabpanel_tab1_HorizontalPanel = new HorizontalPanel();

	final TextBox tabpanel_tab1_TB_interval_1 = new TextBox();

	final TextBox tabpanel_tab1_TB_interval_2 = new TextBox();

	final VerticalPanel tabpanel_tab1_VerticalPanel = new VerticalPanel();

	final Button tabpanel_tab2_Button_1ernum = new Button("(1ernum)", this);

	final Button tabpanel_tab2_Button_eff = new Button("eff", this);

	final Button tabpanel_tab2_Button_eval = new Button("eval()", this);

	final Label tabpanel_tab2_cons = new Label("cons");

	final HorizontalPanel tabpanel_tab2_HorizontalPanel_button = new HorizontalPanel();

	final HorizontalPanel tabpanel_tab2_HorizontalPanel_ecZ = new HorizontalPanel();

	final TextBox tabpanel_tab2_TB0 = new TextBox();

	final TextBox tabpanel_tab2_TB1 = new TextBox();

	final TextBox tabpanel_tab2_TB2 = new TextBox();

	final TextBox tabpanel_tab2_TB3 = new TextBox();

	final TextArea tabpanel_tab2_textArea = new TextArea();

	final VerticalPanel tabpanel_tab2_VerticalPanel = new VerticalPanel();

	final DockPanel tabpanel_tab3_DockPanel = new DockPanel();

	final Button tabpanel_tab3_Button_eval = new Button("eval", this);

	final TextBox tabpanel_tab3_TB_tol = new TextBox();

	final TextBox tabpanel_tab3_TB_f = new TextBox();

	final TextBox tabpanel_tab3_TB_df = new TextBox();

	final TextBox tabpanel_tab3_TB_var = new TextBox();

	final TextBox tabpanel_tab3_TB_var0 = new TextBox();

	final Label tabpanel_tab3_Label_cons_NR = new Label("(cons)");

	final FlexTable tabpanel_tab3_FlexTable_eTaffel_NR = new FlexTable();

	final TextBox tabpanel_tab3_TB_var0_S = new TextBox();

	final TextBox tabpanel_tab3_TB_var1_S = new TextBox();

	final Label tabpanel_tab3_Label_cons_S = new Label("(cons)");

	final FlexTable tabpanel_tab3_FlexTable_eTaffel_S = new FlexTable();

	final VerticalPanel tabpanel_tab3_VerticalPanel_NR = new VerticalPanel();

	final VerticalPanel tabpanel_tab3_VerticalPanel_S = new VerticalPanel();

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// Tab 0
		tabpanel_tab0_randomFill.setChecked(true);
		tabpanel_tab0_randomFill.addClickListener(this);
		llenaTextBoxDeDouble(tabpanel_tab0_matriz_TextBoxes, new double[][] {});
		matriz_ButtonDeNMasMas.setWidth("1cm");
		matriz_ButtonDeNMenosMenos.setWidth("1cm");
		matriz_ScrollPanelDeColumnaDeFilas.setHeight("5" + "cm");
		matriz_ScrollPanelDeColumnaDeFilas.setWidth("5" + "cm");
		tabpanel_tab0_textArea.setSize("9cm", "5cm");
		matriz_ColumnaDeFilas.setSpacing(0);
		matriz_Label_ConNombreDeMat.setText("   A (" + N + "x" + N
				+ ")    =   ");
		tabpanel_tab0_HorizontalPanel_button
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		tabpanel_tab0_VerticalPanel
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		llenaColumnaDeFilas(matriz_ColumnaDeFilas,
				tabpanel_tab0_matriz_TextBoxes);
		matriz_ScrollPanelDeColumnaDeFilas.add(matriz_ColumnaDeFilas);
		matriz_HorizontalPanel_VerticalPanel_ConBotones
				.add(matriz_ButtonDeNMasMas);
		matriz_HorizontalPanel_VerticalPanel_ConBotones.add(tabpanel_tab2_TB2);
		matriz_HorizontalPanel_VerticalPanel_ConBotones
				.add(matriz_ButtonDeNMenosMenos);
		matriz_HorizontalPanel_ParaEcuacioon
				.add(matriz_HorizontalPanel_VerticalPanel_ConBotones);
		matriz_HorizontalPanel_ParaEcuacioon.add(matriz_Label_ConNombreDeMat);
		matriz_HorizontalPanel_ParaEcuacioon
				.add(matriz_ScrollPanelDeColumnaDeFilas);
		tabpanel_tab0_VerticalPanel.add(tabpanel_tab0_randomFill);
		tabpanel_tab0_VerticalPanel.add(matriz_HorizontalPanel_ParaEcuacioon);
		tabpanel_tab0_VerticalPanel.add(tabpanel_tab0_restrict);
		tabpanel_tab0_VerticalPanel.add(tabpanel_tab0_textArea);
		tabpanel_tab0_VerticalPanel.add(tabpanel_tab0_HorizontalPanel_button);
		tabpanel_tab0_HorizontalPanel_button.add(tabpanel_tab0_Button_Inv);
		tabpanel_tab0_HorizontalPanel_button.add(tabpanel_tab0_Button_csv);
		tabpanel_tab0_HorizontalPanel_button.add(tabpanel_tab0_Button_Init);
		tabpanel_tab0_HorizontalPanel_button.add(tabpanel_tab0_Button_Eff);
		tabPanel.add(tabpanel_tab0_VerticalPanel, " Doolittle ");

		// Tab 1

		tabpanel_tab1_TB_plotFunc.setText("ln (1+(cos y)^2)");
		tabpanel_tab1_TB_plotVar.setText("y");
		tabpanel_tab1_TB_interval_1.setText("-pi/2");
		tabpanel_tab1_TB_interval_2.setText("pi/2");
		tabPanel.add(plotFlaeche, " GWT Canvas ");
		tabpanel_tab1_VerticalPanel.add(tabpanel_tab1_HorizontalPanel);
		tabpanel_tab1_VerticalPanel.add(tabpanel_tab1_TB_plotFunc);
		tabpanel_tab1_VerticalPanel.add(tabpanel_tab1_TB_plotVar);
		tabpanel_tab1_VerticalPanel.add(tabpanel_tab1_Button_plotme);
		tabpanel_tab1_HorizontalPanel.add(new Label("intv: { "));
		tabpanel_tab1_HorizontalPanel.add(tabpanel_tab1_TB_interval_1);
		tabpanel_tab1_HorizontalPanel.add(new Label(" , "));
		tabpanel_tab1_HorizontalPanel.add(tabpanel_tab1_TB_interval_2);
		tabpanel_tab1_HorizontalPanel.add(new Label(" } "));
		plotFlaeche.add(tabpanel_tab1_VerticalPanel, DockPanel.CENTER);

		// Tab 2

		tabpanel_tab2_TB1.setText("2");
		tabpanel_tab2_TB2.setWidth("1cm");
		tabpanel_tab2_TB2.addKeyboardListener(this);
		tabpanel_tab2_TB2.setText((new Integer(N)).toString());
		tabPanel.add(tabpanel_tab2_VerticalPanel, " parser.eval() ");
		tabpanel_tab2_VerticalPanel.add(tabpanel_tab2_textArea);
		tabpanel_tab2_textArea.setSize("9cm", "5cm");
		tabpanel_tab2_VerticalPanel.add(tabpanel_tab2_cons);
		tabpanel_tab2_HorizontalPanel_button
				.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		tabpanel_tab2_HorizontalPanel_button
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		tabpanel_tab2_HorizontalPanel_button.add(tabpanel_tab2_Button_eval);
		tabpanel_tab2_HorizontalPanel_button.add(tabpanel_tab2_Button_eff);
		tabpanel_tab2_HorizontalPanel_button.add(tabpanel_tab2_Button_1ernum);
		tabpanel_tab2_HorizontalPanel_ecZ
				.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		tabpanel_tab2_HorizontalPanel_ecZ
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		tabpanel_tab2_HorizontalPanel_ecZ.add(tabpanel_tab2_z);
		tabpanel_tab2_HorizontalPanel_ecZ.add(tabpanel_tab2_TB1);
		tabpanel_tab2_VerticalPanel.add(tabpanel_tab2_HorizontalPanel_button);
		tabpanel_tab2_VerticalPanel.add(tabpanel_tab2_HorizontalPanel_ecZ);
		tabpanel_tab2_TB0.setText("ln exp(- -z[0]/2+-ln 1 *8^1.0E-006)");
		tabpanel_tab2_VerticalPanel.add(tabpanel_tab2_TB0);
		tabpanel_tab2_VerticalPanel
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		tabpanel_tab2_VerticalPanel.setSpacing(0);
		tabpanel_tab2_VerticalPanel
				.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		tabpanel_tab2_TB0.addKeyboardListener(this);

		// Tab 3

		tabpanel_tab3_TB_var.setText("x[0]");
		tabpanel_tab3_TB_var0.setText("0");
		tabpanel_tab3_TB_f.setText("cos x[0] -x[0]");
		tabpanel_tab3_TB_df.setText("-sin x[0] -1");
		tabpanel_tab3_TB_var0_S.setText("0");
		tabpanel_tab3_TB_var1_S.setText("-1");
		tabpanel_tab3_TB_tol.setText("1/10000");

		tabpanel_tab3_VerticalPanel_NR
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		tabpanel_tab3_VerticalPanel_NR
				.add(new HTML(
						"<html><head></head><body><span style='font-weight: bold; font-size: 29pt;'>NR...</span></body></html>"));
		tabpanel_tab3_VerticalPanel_S
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		tabpanel_tab3_VerticalPanel_S
				.add(new HTML(
						"<html><head></head><body><span style='font-weight: bold; font-size: 29pt;'>S...</span></body></html>"));

		tabpanel_tab3_VerticalPanel_NR.add(new Label("var<0>: "));
		tabpanel_tab3_VerticalPanel_NR.add(tabpanel_tab3_TB_var0);
		tabpanel_tab3_VerticalPanel_NR.add(new Label("(df/dvar)|(var<n>)="));
		tabpanel_tab3_VerticalPanel_NR.add(tabpanel_tab3_TB_df);
		tabpanel_tab3_VerticalPanel_NR.add(tabpanel_tab3_FlexTable_eTaffel_NR);
		tabpanel_tab3_VerticalPanel_NR.add(tabpanel_tab3_Label_cons_NR);

		tabpanel_tab3_VerticalPanel_S.add(new Label("var<0>: "));
		tabpanel_tab3_VerticalPanel_S.add(tabpanel_tab3_TB_var0_S);
		tabpanel_tab3_VerticalPanel_S.add(new Label("var<1>: "));
		tabpanel_tab3_VerticalPanel_S.add(tabpanel_tab3_TB_var1_S);
		tabpanel_tab3_VerticalPanel_S.add(tabpanel_tab3_FlexTable_eTaffel_S);
		tabpanel_tab3_VerticalPanel_S.add(tabpanel_tab3_Label_cons_S);

		tabpanel_tab3_DockPanel
				.add(tabpanel_tab3_Button_eval, DockPanel.CENTER);
		tabpanel_tab3_DockPanel.add(new Label("var: "), DockPanel.NORTH);
		tabpanel_tab3_DockPanel.add(tabpanel_tab3_TB_var, DockPanel.NORTH);
		tabpanel_tab3_DockPanel.add(new Label("f(var<n>)= 0 ="),
				DockPanel.NORTH);
		tabpanel_tab3_DockPanel.add(tabpanel_tab3_TB_f, DockPanel.NORTH);
		tabpanel_tab3_DockPanel.add(new Label("tol: "), DockPanel.NORTH);
		tabpanel_tab3_DockPanel.add(tabpanel_tab3_TB_tol, DockPanel.NORTH);
		tabpanel_tab3_DockPanel.add(tabpanel_tab3_VerticalPanel_S,
				DockPanel.EAST);
		tabpanel_tab3_DockPanel.add(tabpanel_tab3_VerticalPanel_NR,
				DockPanel.WEST);
		tabPanel.add(tabpanel_tab3_DockPanel, "NR,S");

		// Assume that the host HTML has elements defined whose
		// IDs are "slot1", "slot2". In a real app, you probably would not want
		// to hard-code IDs. Instead, you could, for example, search for all
		// elements with a particular CSS class and replace them with widgets.
		//

		tabPanel.setStyleName("myStyles");
		tabPanel.selectTab(1);
		tabPanel.setHeight("100%");
		tabPanel.setWidth("100%");
		main.setSize("10" + "cm", "15" + "cm");
		main.add(tabPanel, DockPanel.CENTER);
		RootPanel.get("slot3").add(main);
	}

	public static TextBox[][] generaTextBoxes(int Nx, int Ny, String Length) {
		TextBox[][] matriz_TextBoxes = new TextBox[Nx][Ny];
		for (int i = 0; i < matriz_TextBoxes.length; i++) {
			for (int j = 0; j < matriz_TextBoxes[i].length; j++) {
				matriz_TextBoxes[i][j] = new TextBox();
				matriz_TextBoxes[i][j].setWidth(Length);
			}
		}
		return matriz_TextBoxes;
	}

	final Label tabpanel_tab2_z = new Label(" z[20] = ");

	public void generaColumnaDeFilas(VerticalPanel matriz_ColumnaDeFilas,
			int N, String length) {
		final TextBox[][] matriz_TextBoxes = new TextBox[N][N];
		final HorizontalPanel[] matriz_Filas = new HorizontalPanel[N];
		// TODO: a una columna ponerle filas
		for (int i = 0; i < matriz_TextBoxes.length; i++) {
			matriz_Filas[i] = new HorizontalPanel();
			for (int j = 0; j < matriz_TextBoxes[i].length; j++) {
				matriz_TextBoxes[i][j] = new TextBox();
				matriz_TextBoxes[i][j].setWidth(length);
				matriz_Filas[i].add(matriz_TextBoxes[i][j]);
			}
			matriz_ColumnaDeFilas.add(matriz_Filas[i]);
		}
	}

	public void llenaColumnaDeFilas(VerticalPanel matriz_ColumnaDeFilas,
			TextBox[][] matriz_TextBoxes) {
		matriz_ColumnaDeFilas.clear();
		final HorizontalPanel[] matriz_Filas = new HorizontalPanel[matriz_TextBoxes.length];
		// TODO: a una columna ponerle filas
		for (int i = 0; i < matriz_TextBoxes.length; i++) {
			matriz_Filas[i] = new HorizontalPanel();
			for (int j = 0; j < matriz_TextBoxes[i].length; j++) {
				matriz_Filas[i].add(matriz_TextBoxes[i][j]);
			}
			matriz_ColumnaDeFilas.add(matriz_Filas[i]);
		}
	}

	public void llenaTextBoxDeDouble(TextBox[][] TBA, double[][] matriz) {
		A=matriz;
		for (int i = 0; i < matriz.length; i++) {
			for (int j = 0; j < matriz[i].length; j++) {
				TBA[i][j].setText(String.valueOf(matriz[i][j]));
			}
		}
		if (TBA.length > matriz.length
				&& tabpanel_tab0_randomFill.isChecked() == true) {
			A=new double[TBA.length][TBA.length];
			for (int i = 0; i < TBA.length; i++) {
				for (int j = 0; j < TBA[i].length; j++) {
					if (i >= matriz.length
							|| (i < matriz.length && j >= matriz[i].length)) {
						A[i][j]=new Double(Math.random()* Math.pow(-1, Math.round(2 * (Math.random() - 1)))).doubleValue();
						TBA[i][j].setText(new Double(A[i][j]).toString());
					}
				}
			}
		}
		return;
	}

	public void onClick(Widget sender) {
		if (sender == (tabpanel_tab0_Button_Inv)) {
			tabpanel_tab0_restrict.setText("1<n<100");
			int n = 0;
			double[][] B;
			try {
				n = (new Integer(tabpanel_tab2_TB2.getText())).intValue();
			} catch (NumberFormatException e) {
				tabpanel_tab0_restrict.setText("1<n<100!!!!");
			}
			if (n != tabpanel_tab0_matriz_TextBoxes.length) {
				if (n >= 1 && n < MAX_N) {
					N = n;

					matriz_Label_ConNombreDeMat.setText("   A (" + N + "x" + N
							+ ")    =   ");
					tabpanel_tab0_matriz_TextBoxes = generaTextBoxes(N, N,
							"1cm");
					llenaColumnaDeFilas(matriz_ColumnaDeFilas,
							tabpanel_tab0_matriz_TextBoxes);
					llenaTextBoxDeDouble(tabpanel_tab0_matriz_TextBoxes,
							new double[][] {});
					matriz_HorizontalPanel_ParaEcuacioon.clear();
					matriz_HorizontalPanel_ParaEcuacioon
							.add(matriz_HorizontalPanel_VerticalPanel_ConBotones);
					matriz_HorizontalPanel_ParaEcuacioon
							.add(matriz_Label_ConNombreDeMat);
					matriz_HorizontalPanel_ParaEcuacioon
							.add(matriz_ScrollPanelDeColumnaDeFilas);
					tabpanel_tab2_TB2.setText((new Integer(N)).toString());
				} else {
					tabpanel_tab0_restrict.setText("1<n<100!!!!");
				}
			}
			if (n >= 1 && n < MAX_N) {
				B=new double[A.length][A.length];
				B = NumerischeOperationen.inversa(NumerischeOperationen
						.Identidad(n),
						A);
				tabpanel_tab0_textArea
						.setText("\n A= \n"
								+ NumerischeOperationen
										.imprimeMatriz(NumerischeOperationen
												.double2Double(tomaDoubleDeTextBox(tabpanel_tab0_matriz_TextBoxes)))
								+ "\n inv(A) = \n"
								+ NumerischeOperationen
										.imprimeMatriz(NumerischeOperationen
												.double2Double(NumerischeOperationen
														.prodtcEscalar(1, B))));
				llenaTextBoxDeDouble(tabpanel_tab0_matriz_TextBoxes, B);
			}
		}
		if (sender == tabpanel_tab0_Button_Eff) {
			tabpanel_tab0_textArea.setText("");
			for (int i = 0; i < tabpanel_tab0_matriz_TextBoxes.length; i++) {
				for (int j = 0; j < tabpanel_tab0_matriz_TextBoxes[i].length; j++) {
					tabpanel_tab0_matriz_TextBoxes[i][j].setText("");
				}
			}
		}
		if (sender == tabpanel_tab0_Button_Init) {
			llenaTextBoxDeDouble(tabpanel_tab0_matriz_TextBoxes,
					new double[][] { { -1, 2, 3, 4 }, { 5, 6, 7, 8 },
							{ 9, 10, 11, 12 }, { 13, 14, 15, -16 } });
		}
		if (sender == matriz_ButtonDeNMasMas) {
			if (N < MAX_N) {
				N = N + 1;
			}
			matriz_Label_ConNombreDeMat.setText("   A (" + N + "x" + N
					+ ")    =   ");
			tabpanel_tab0_matriz_TextBoxes = generaTextBoxes(N, N, "1cm");
			llenaColumnaDeFilas(matriz_ColumnaDeFilas,
					tabpanel_tab0_matriz_TextBoxes);
			llenaTextBoxDeDouble(tabpanel_tab0_matriz_TextBoxes,
					new double[][] {});
			matriz_HorizontalPanel_ParaEcuacioon.clear();
			matriz_HorizontalPanel_ParaEcuacioon
					.add(matriz_HorizontalPanel_VerticalPanel_ConBotones);
			matriz_HorizontalPanel_ParaEcuacioon
					.add(matriz_Label_ConNombreDeMat);
			matriz_HorizontalPanel_ParaEcuacioon
					.add(matriz_ScrollPanelDeColumnaDeFilas);
			tabpanel_tab2_TB2.setText((new Integer(N)).toString());
		}
		if (sender == matriz_ButtonDeNMenosMenos) {
			if (N > 2) {
				N = N - 1;
			}
			matriz_Label_ConNombreDeMat.setText("   A (" + N + "x" + N
					+ ")    =   ");
			tabpanel_tab0_matriz_TextBoxes = generaTextBoxes(N, N, "1cm");
			llenaColumnaDeFilas(matriz_ColumnaDeFilas,
					tabpanel_tab0_matriz_TextBoxes);
			llenaTextBoxDeDouble(tabpanel_tab0_matriz_TextBoxes,
					new double[][] {});
			matriz_HorizontalPanel_ParaEcuacioon.clear();
			matriz_HorizontalPanel_ParaEcuacioon
					.add(matriz_HorizontalPanel_VerticalPanel_ConBotones);
			matriz_HorizontalPanel_ParaEcuacioon
					.add(matriz_Label_ConNombreDeMat);
			matriz_HorizontalPanel_ParaEcuacioon
					.add(matriz_ScrollPanelDeColumnaDeFilas);
			tabpanel_tab2_TB2.setText((new Integer(N)).toString());
		}
		if (sender == tabpanel_tab0_randomFill) {
			if (tabpanel_tab0_randomFill.isChecked() == true) {
				llenaTextBoxDeDouble(tabpanel_tab0_matriz_TextBoxes,
						new double[][] {});
			} else {
				tabpanel_tab0_textArea.setText("");
				for (int i = 0; i < tabpanel_tab0_matriz_TextBoxes.length; i++) {
					for (int j = 0; j < tabpanel_tab0_matriz_TextBoxes[i].length; j++) {
						tabpanel_tab0_matriz_TextBoxes[i][j].setText("");
					}
				}
			}
		}
		if (sender == tabpanel_tab1_Button_plotme) {
			Variable var = new Variable(tabpanel_tab1_TB_plotVar.getText(), 0.0);
			Variable pi = new Variable("pi", Math.PI);
			RecursiveDescentParser parser = new RecursiveDescentParser(
					tabpanel_tab1_TB_plotFunc.getText(), new Variable[] { var,
							pi });
			float[][] puntos = new float[25][2];
			float temp = 0;
			float miin = (float) (new RecursiveDescentParser(
					tabpanel_tab1_TB_interval_1.getText(), new Variable[] {
							var, pi }).eval());
			float maax = (float) (new RecursiveDescentParser(
					tabpanel_tab1_TB_interval_2.getText(), new Variable[] {
							var, pi }).eval());
			for (int i = 0; i < puntos.length; i++) {
				parser.wiederaufstellen();
				puntos[i][0] = miin + ((float) i / (float) (puntos.length - 1))
						* (maax - miin);
				var.setGegenwert(new Float(puntos[i][0]));
				temp = (float) parser.eval();
				if (temp != 0.0 / 0.0) {
					puntos[i][1] = temp;
				} else {
					puntos[i][1] = (float) 0.0;
				}
			}
			Canvas canvas = new Canvas(800, 100);
			AbsolutePanel panel = Graphes.listPlot2DFlaeche(canvas);
			plotFlaeche.add(panel, DockPanel.SOUTH);
			plotFlaeche.add(new Label("{ow,oh}={" + canvas.getOffsetWidth()
					+ "," + canvas.getOffsetHeight() + "}" + "\nAL:"
					+ canvas.getAbsoluteLeft() + "\nAT:"
					+ canvas.getAbsoluteTop()), DockPanel.SOUTH);
			Graphes.listPlot2D(puntos, canvas, null, new float[] {
					(float) 0.001, (float) 0.001 });
			// VeraenderbareTafel VT = new VeraenderbareTafel(50, 50);
			// for (int i = 0; i < VT.getRowCount(); i++) {
			// for (int j = 0; j < VT.getColumnCount(); j++) {
			// VT.setText(i, j, "hola");
			// }
			// }
			// plotFlaeche.add(VT, DockPanel.EAST);
		}
		if (sender == tabpanel_tab2_Button_1ernum) {
			RecursiveDescentParser parser1 = new RecursiveDescentParser(
					tabpanel_tab2_TB0.getText());
			tabpanel_tab2_cons.setText((new Double(parser1.einDoubleLesen()))
					.toString());
			if (parser1.isFehler() == true) {
				tabpanel_tab2_cons.setText(tabpanel_tab2_cons.getText()
						+ parser1.getFehlerGeschehen());
			}
			parser1.wiederaufstellen();
		}
		if (sender == tabpanel_tab2_Button_eff) {
			tabpanel_tab2_textArea.setText("");
			// tabpanel_tab2_TB0.setText("");
		}
		if (sender == tabpanel_tab2_Button_eval) {// eval()
			onKeyUp(tabpanel_tab2_TB0, '\n', KeyboardListener.MODIFIER_ALT);
		}
		if (sender == tabpanel_tab0_Button_csv) {
			tabpanel_tab0_restrict.setText("1<n<100");
			int n = 0;
			double[][] B;
			try {
				n = (new Integer(tabpanel_tab2_TB2.getText())).intValue();
			} catch (NumberFormatException e) {
				tabpanel_tab0_restrict.setText("1<n<100!!!!");
			}
			if (n != tabpanel_tab0_matriz_TextBoxes.length) {
				if (n >= 1 && n < MAX_N) {
					N = n;

					matriz_Label_ConNombreDeMat.setText("   A (" + N + "x" + N
							+ ")    =   ");
					tabpanel_tab0_matriz_TextBoxes = generaTextBoxes(N, N,
							"1cm");
					llenaColumnaDeFilas(matriz_ColumnaDeFilas,
							tabpanel_tab0_matriz_TextBoxes);
					llenaTextBoxDeDouble(tabpanel_tab0_matriz_TextBoxes,
							new double[][] {});
					matriz_HorizontalPanel_ParaEcuacioon.clear();
					matriz_HorizontalPanel_ParaEcuacioon
							.add(matriz_HorizontalPanel_VerticalPanel_ConBotones);
					matriz_HorizontalPanel_ParaEcuacioon
							.add(matriz_Label_ConNombreDeMat);
					matriz_HorizontalPanel_ParaEcuacioon
							.add(matriz_ScrollPanelDeColumnaDeFilas);
					tabpanel_tab2_TB2.setText((new Integer(N)).toString());
				} else {
					tabpanel_tab0_restrict.setText("1<n<100!!!!");
				}
			}
			if (n >= 1 && n < MAX_N) {
				B = NumerischeOperationen.inversa(NumerischeOperationen
						.Identidad(n),
						tomaDoubleDeTextBox(tabpanel_tab0_matriz_TextBoxes));
				tabpanel_tab0_textArea
						.setText("\n A= \n"
								+ NumerischeOperationen
										.imprimeMatriz(
												NumerischeOperationen
														.double2Double(tomaDoubleDeTextBox(tabpanel_tab0_matriz_TextBoxes)),
												12, 3, false, new char[] { ' ',
														' ' })
								+ "\n inv(A) = \n"
								+ NumerischeOperationen
										.imprimeMatriz(
												NumerischeOperationen
														.double2Double(NumerischeOperationen
																.prodtcEscalar(
																		1, B)),
												5, 3, false, new char[] { ' ',
														' ' }));
				llenaTextBoxDeDouble(tabpanel_tab0_matriz_TextBoxes, B);
			}
		}
		if (sender == tabpanel_tab3_Button_eval) {
			Variable pi = new Variable("pi", Math.PI);
			Double tol = new Double(new RecursiveDescentParser(
					tabpanel_tab3_TB_tol.getText(), new Variable[] { pi })
					.eval());
			Double var0 = new Double(new RecursiveDescentParser(
					tabpanel_tab3_TB_var0.getText(), new Variable[] { pi })
					.eval());
			Double var0_S = new Double(new RecursiveDescentParser(
					tabpanel_tab3_TB_var0_S.getText(), new Variable[] { pi })
					.eval());
			Double var1_S = new Double(new RecursiveDescentParser(
					tabpanel_tab3_TB_var1_S.getText(), new Variable[] { pi })
					.eval());
			Variable var = new Variable(tabpanel_tab3_TB_var.getText(), var0
					.doubleValue());
			AnalisierteFunktion f = new AnalisierteFunktion(tabpanel_tab3_TB_f
					.getText(), new Variable[] { pi, var });
			AnalisierteFunktion df = new AnalisierteFunktion(
					tabpanel_tab3_TB_df.getText(), new Variable[] { pi, var });

			tabpanel_tab3_FlexTable_eTaffel_NR.setWidget(0, 0, new Label("n"));
			tabpanel_tab3_FlexTable_eTaffel_NR.setWidget(0, 1, new Label(
					"var<n>"));
			tabpanel_tab3_FlexTable_eTaffel_S.setWidget(0, 0, new Label("n"));
			tabpanel_tab3_FlexTable_eTaffel_S.setWidget(0, 1, new Label(
					"var<n>"));
			Number[] erg = new Number[200];
			int i = 0;
			boolean fehlung = true;

			// NR
			while (i < erg.length - 1 && fehlung) {
				try {
					erg[i] = NumerischeMethoden
							.NewtonmethodeFuerEineEinzigeVariable(f, var, df,
									var0, tol, i);
					fehlung = false;
				} catch (FehlungDerVariable e) {
					tabpanel_tab3_Label_cons_NR.setText(e.getMessage());
				} catch (FehlungDerMethode e) {
					erg[i] = e.getSWert();
				}
				tabpanel_tab3_FlexTable_eTaffel_NR.setHTML(i + 1, 0, "" + (i));
				tabpanel_tab3_FlexTable_eTaffel_NR.setHTML(i + 1, 1,
						NumerischeOperationen.notacioonCientiifica(erg[i]
								.doubleValue(), 5, 3));
				i++;
			}
			tabpanel_tab3_Label_cons_NR.setText("");

			// S
			erg = new Number[MAXITS];
			i = 0;
			fehlung = true;
			while (i < erg.length - 1 && fehlung) {
				try {
					erg[i] = NumerischeMethoden.Sekant(f, var, var0_S, var1_S,
							tol, i);
					fehlung = false;
				} catch (FehlungDerVariable e) {
					tabpanel_tab3_Label_cons_S.setText(e.getMessage());
				} catch (FehlungDerMethode e) {
					erg[i] = e.getSWert();
				}
				tabpanel_tab3_FlexTable_eTaffel_S.setHTML(i + 1, 0, "" + (i));
				tabpanel_tab3_FlexTable_eTaffel_S.setHTML(i + 1, 1,
						NumerischeOperationen.notacioonCientiifica(erg[i]
								.doubleValue(), 5, 3));
				i++;
			}
			tabpanel_tab3_Label_cons_S.setText("");
		}
	}

	public void onKeyDown(Widget sender, char keyCode, int modifiers) {

	}

	public void onKeyPress(Widget sender, char keyCode, int modifiers) {

	}

	public void onKeyUp(Widget sender, char keyCode, int modifiers) {
		if (sender == tabpanel_tab2_TB0) {// eval()
			RecursiveDescentParser parser1 = new RecursiveDescentParser(
					tabpanel_tab2_TB0.getText());
			parser1.setVariable(new Variable[] {
					new Variable("x", 0.0),
					new Variable("y", 0.0),
					new Variable("z", 20, (new RecursiveDescentParser(
							tabpanel_tab2_TB1.getText())).eval()) });
			parser1.setKonstante(new Variable[]{new Variable("e",Math.E),new Variable("pi",Math.PI)});
			tabpanel_tab2_cons.setText((new Double(parser1.eval())).toString());
			tabpanel_tab2_textArea.setText(tabpanel_tab2_textArea.getText()
					+ "\n" + parser1.ausDruckOhneHohler(true) + " = "
					+ (new Double(parser1.eval())).toString());
			if (parser1.isFehler() == true) {
				tabpanel_tab2_cons.setText(tabpanel_tab2_cons.getText() + "..."
						+ parser1.getFehlerGeschehen());
			}
			parser1.wiederaufstellen();
		}
		if (keyCode == KeyboardListener.KEY_ENTER
				&& sender == tabpanel_tab2_TB2) {
			tabpanel_tab0_restrict.setText("1<n<100");
			int num = 0;
			try {
				num = (new Integer(tabpanel_tab2_TB2.getText())).intValue();
			} catch (NumberFormatException e) {
				tabpanel_tab0_restrict.setText("1<n<100!!!!");
			}
			if (num >= 1 && num < MAX_N) {
				N = num;

				matriz_Label_ConNombreDeMat.setText("   A (" + N + "x" + N
						+ ")    =   ");
				tabpanel_tab0_matriz_TextBoxes = generaTextBoxes(N, N, "1cm");
				llenaColumnaDeFilas(matriz_ColumnaDeFilas,
						tabpanel_tab0_matriz_TextBoxes);
				llenaTextBoxDeDouble(tabpanel_tab0_matriz_TextBoxes,
						new double[][] {});
				matriz_HorizontalPanel_ParaEcuacioon.clear();
				matriz_HorizontalPanel_ParaEcuacioon
						.add(matriz_HorizontalPanel_VerticalPanel_ConBotones);
				matriz_HorizontalPanel_ParaEcuacioon
						.add(matriz_Label_ConNombreDeMat);
				matriz_HorizontalPanel_ParaEcuacioon
						.add(matriz_ScrollPanelDeColumnaDeFilas);
				tabpanel_tab2_TB2.setText((new Integer(N)).toString());
			} else {
				tabpanel_tab0_restrict.setText("1<n<100!!!!");
			}
		}
	}

	public void reiniciaTextBoxes(TextBox[][] matriz_TextBoxes, int Nx, int Ny,
			String Length) {
		matriz_TextBoxes = new TextBox[Nx][Ny];
		for (int i = 0; i < Nx; i++) {
			matriz_TextBoxes[i] = new TextBox[Ny];
			for (int j = 0; j < Ny; j++) {
				matriz_TextBoxes[i][j] = new TextBox();
				matriz_TextBoxes[i][j].setWidth(Length);
			}
		}
	}

	public double[][] tomaDoubleDeTextBox(TextBox[][] TBA) {
		double[][] resultado = new double[TBA.length][];
		for (int i = 0; i < TBA.length; i++) {
			resultado[i] = new double[TBA[i].length];
			for (int j = 0; j < TBA[i].length; j++) {
				resultado[i][j] = 0.0 / 0.0;
				resultado[i][j] = new Double(TBA[i][j].getText()).doubleValue();
			}
		}
		return resultado;
	}

}
